package com.bookcycle.panda.webfront.interceptor;

import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Login;
import com.baomidou.kisso.common.util.HttpUtil;
import com.baomidou.kisso.security.token.SSOToken;
import com.baomidou.kisso.web.handler.KissoDefaultHandler;
import com.baomidou.kisso.web.handler.SSOHandlerInterceptor;
import com.bookcycle.panda.webfront.constant.URLConstant;
import com.bookcycle.panda.webfront.doo.UserDO;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mason
 * @date 2018/3/25
 */
public class MyInterceptor implements HandlerInterceptor {
    private SSOHandlerInterceptor handlerInterceptor;

    public SSOHandlerInterceptor getHandlerInterceptor() {
        return (SSOHandlerInterceptor)(this.handlerInterceptor == null ? KissoDefaultHandler.getInstance() : this.handlerInterceptor);
    }

    public void setHandlerInterceptor(SSOHandlerInterceptor handlerInterceptor) {
        this.handlerInterceptor = handlerInterceptor;
    }
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        SSOToken ssoToken=SSOHelper.getSSOToken(request);

        if(ssoToken==null){
           //response.sendRedirect("http://111.230.107.192:8124/signIn");
            response.sendRedirect("/signIn");
           return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        SSOToken ssoToken=SSOHelper.getSSOToken(request);
        if(ssoToken!=null){
            String userId=ssoToken.getId();
            RestTemplate restTemplate=new RestTemplate();
            MultiValueMap multiValueMap=new LinkedMultiValueMap();
            multiValueMap.add("userId",userId);
            ResponseEntity<UserDO> responseEntity=restTemplate.postForEntity(URLConstant.URL_GET_USER_BY_ID,multiValueMap,UserDO.class);
            /**非ajax请求*/
            if(!"XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
                modelAndView.addObject("avatar",responseEntity.getBody().getAvatar());
            }
        }else {
            modelAndView.addObject("avatar",null);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
